<template>
  <div class="environment-config">
    <div class="header-actions">
      <h1>环境配置</h1>
      <el-button type="primary" @click="saveConfig" :loading="saving">保存配置</el-button>
    </div>

    <el-card class="mb-4">
      <template #header>
        <div class="card-header">
          <span>拓扑图编辑器</span>
        </div>
      </template>

      <div class="topology-editor">
        <div class="topology-container" ref="topologyContainer">
          <div class="placeholder-text" v-if="!hasNodes">
            拖拽节点到此区域创建拓扑图
          </div>
        </div>
        <div class="node-tools">
          <el-button type="primary" icon="Plus" @click="addVirtualMachine">添加虚拟机</el-button>
          <el-button type="primary" icon="Plus" @click="addContainer">添加容器</el-button>
        </div>
      </div>
    </el-card>

    <el-card>
      <template #header>
        <div class="card-header">
          <span>节点参数配置</span>
        </div>
      </template>

      <el-form :model="nodeConfig" label-width="120px">
        <el-form-item label="IP 地址">
          <el-input v-model="nodeConfig.ip" placeholder="请输入 IP 地址" />
        </el-form-item>

        <el-form-item label="节点类型">
          <el-select v-model="nodeConfig.type" placeholder="请选择节点类型">
            <el-option label="虚拟机" value="virtual_machine" />
            <el-option label="容器" value="container" />
          </el-select>
        </el-form-item>

        <el-form-item label="操作系统版本">
          <el-select v-model="nodeConfig.osVersion" placeholder="请选择操作系统版本">
            <el-option label="Ubuntu 20.04 LTS" value="Ubuntu 20.04 LTS" />
            <el-option label="Ubuntu 18.04 LTS" value="Ubuntu 18.04 LTS" />
            <el-option label="CentOS 7" value="CentOS 7" />
            <el-option label="Windows Server 2019" value="Windows Server 2019" />
            <el-option label="Windows Server 2016" value="Windows Server 2016" />
          </el-select>
        </el-form-item>

        <el-form-item label="依赖配置">
          <el-input
            v-model="nodeConfig.dependencies"
            type="textarea"
            :rows="4"
            placeholder="请输入依赖配置信息"
          />
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { getEnvironmentConfig, saveEnvironmentConfig } from '@/api/topo/vulnEnvironment'

// 数据和状态
const saving = ref(false)
const topologyContainer = ref(null)
const nodes = ref([])

// 节点配置
const nodeConfig = reactive({
  ip: '',
  type: 'virtual_machine',
  osVersion: 'Ubuntu 20.04 LTS',
  dependencies: ''
})

// 是否有节点
const hasNodes = computed(() => nodes.value.length > 0)

// 加载配置
const loadConfig = async () => {
  try {
    const { data } = await getEnvironmentConfig()
    if (data && data.nodes) {
      nodes.value = data.nodes
      // 如果有节点，选择第一个节点显示详情
      if (data.nodes.length > 0) {
        Object.assign(nodeConfig, data.nodes[0])
      }
    }
  } catch (error) {
    console.error('加载环境配置失败', error)
    ElMessage.error('加载环境配置失败')
  }
}

// 保存配置
const saveConfig = async () => {
  saving.value = true
  try {
    await saveEnvironmentConfig({
      nodes: nodes.value
    })
    ElMessage.success('配置保存成功')
  } catch (error) {
    console.error('保存环境配置失败', error)
    ElMessage.error('保存环境配置失败')
  } finally {
    saving.value = false
  }
}

// 添加虚拟机节点
const addVirtualMachine = () => {
  const newNode = {
    id: Date.now().toString(),
    name: `虚拟机-${nodes.value.length + 1}`,
    type: 'virtual_machine',
    ip: '',
    osVersion: 'Ubuntu 20.04 LTS',
    dependencies: '',
    position: { x: 100, y: 100 }
  }
  
  nodes.value.push(newNode)
  Object.assign(nodeConfig, newNode)
}

// 添加容器节点
const addContainer = () => {
  const newNode = {
    id: Date.now().toString(),
    name: `容器-${nodes.value.length + 1}`,
    type: 'container',
    ip: '',
    osVersion: 'Ubuntu 20.04 LTS',
    dependencies: '',
    position: { x: 100, y: 250 }
  }
  
  nodes.value.push(newNode)
  Object.assign(nodeConfig, newNode)
}

// 初始化
onMounted(() => {
  loadConfig()
})
</script>

<style lang="scss" scoped>
.environment-config {
  padding: 20px;
  
  .topology-editor {
    display: flex;
    flex-direction: column;
    gap: 16px;
    
    .topology-container {
      width: 100%;
      height: 400px;
      border: 1px dashed #dcdfe6;
      border-radius: 4px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #f8f9fa;
      
      .placeholder-text {
        color: #909399;
        font-size: 14px;
      }
    }
    
    .node-tools {
      display: flex;
      gap: 10px;
    }
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 